package com.vancouver.knows.business.tracker;

import java.util.List;

import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.CityNewsDetailInfo;
import com.vancouver.knows.business.bean.EventDetailInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.business.bean.PostFollowerUserInfo;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.bean.UserPostInfo;
import com.vancouver.knows.business.task.mark.UserDeleteFollowTaskMark;

/**
 * 取消关注
 * @author chenxunlin01
 * 
 */
public class UserDeleteFollowTracker extends ABInvokeTracker {

	public UserDeleteFollowTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	public String TAG() {
		return UserDeleteFollowTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		UserDeleteFollowTaskMark taskMark = (UserDeleteFollowTaskMark) result.getTaskMark();
		CityNewsDetailInfo cityNewsDetailInfo = module.getCacheManager().getCityNewsDetailInfoMap()
				.get(taskMark.getPostId());
		if (cityNewsDetailInfo != null) {
			cityNewsDetailInfo.setUserFollowed("N");
		}

		EventDetailInfo eventDetailInfo = module.getCacheManager().getEventDetailInfoMap().get(taskMark.getPostId());
		if (eventDetailInfo != null) {
			eventDetailInfo.setUserFollowed("N");
			UserInfo userInfo = module.getCacheManager().getUserInfo();
			List<PostFollowerUserInfo> data = eventDetailInfo.getEventDetailUserInfoList();
			if (data != null) {
				for (PostFollowerUserInfo eventDetailUserInfo : data) {
					if (eventDetailUserInfo.getId().equals(userInfo.getUserID())) {
						data.remove(eventDetailUserInfo);
						eventDetailInfo.setFollowCount(eventDetailInfo.getFollowCount() - 1 <= 0 ? 0 : eventDetailInfo
								.getFollowCount() - 1);
					}
				}
			}
		}

		CityDiscountDetailInfo cityDiscountDetailInfo = module.getCacheManager().getDiscountDetailInfoMap()
				.get(taskMark.getPostId());
		if (cityDiscountDetailInfo != null) {
			cityDiscountDetailInfo.setUserFollowed("N");
		}

		UserPostInfo userPostInfo = module.getCacheManager().getUserPostsInfoCache().getItemInfoById(taskMark.getPostId());
		if (userPostInfo != null) {
			userPostInfo.setUserFollowed("N");
			userPostInfo.decrementFollowCount();
		}

		// 刪除我关注的内容
		module.getCacheManager().getMyFollowedCache().removeItemFromIds(taskMark.getPostId());

		imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_USER_DELETE_FOLLOW);
	}
}
